package com.example.swd1.gudong.main.user.historyrecord;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.model.Record;

import java.util.List;
import java.util.Map;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class HistoryRecordListAdapter extends BaseAdapter {
    List<Record> records;
    Map<Integer, Float> monthSumValue;
    Context context;

    public HistoryRecordListAdapter(Context context, List<Record> records, Map<Integer, Float> monthSumValue) {
        this.records = records;
        this.monthSumValue = monthSumValue;
        this.context = context;
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.history_item, null);
            viewHolder.timeView = (TextView) convertView.findViewById(R.id.tv_history_item_time);
            viewHolder.distanceView = (TextView) convertView.findViewById(R.id.tv_history_item_distance);
            viewHolder.hoursView = (TextView) convertView.findViewById(R.id.tv_history_item_hours);
            viewHolder.paceView = (TextView) convertView.findViewById(R.id.tv_history_item_pace);
            viewHolder.monthView = (TextView) convertView.findViewById(R.id.tv_history_item_month);
            viewHolder.monthSumView = (TextView) convertView.findViewById(R.id.tv_history_item_month_sum);
            viewHolder.sum = convertView.findViewById(R.id.rl_history_sum);
            convertView.setTag(viewHolder);
        } else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        Record record = records.get(position);
        if (record.isFirstOfMonth()) {
            viewHolder.monthView.setText(record.getMonthStr());
            float sumF = monthSumValue.get(record.getYearAndDay());
            int sumI= (int) (sumF*100);
            viewHolder.monthSumView.setText( sumI/100.0+ "公里");
            viewHolder.sum.setVisibility(View.VISIBLE);
        } else {
            viewHolder.sum.setVisibility(View.GONE);
        }
        viewHolder.timeView.setText(record.getTime());
        viewHolder.distanceView.setText(record.getDistance());
        viewHolder.hoursView.setText(record.getHoursStr());
        viewHolder.paceView.setText(record.getAvgPaceStr());
        return convertView;
    }

    private class ViewHolder {
        private TextView timeView;
        private TextView distanceView;
        private TextView hoursView;
        private TextView paceView;
        private TextView monthView;
        private TextView monthSumView;
        private View sum;
    }
}
